class Libsecret < Formula
  desc "Library for storing/retrieving passwords and other secrets"
  homepage "https://wiki.gnome.org/Projects/Libsecret"
  url "https://download.gnome.org/sources/libsecret/0.21/libsecret-0.21.7.tar.xz"
  sha256 "6b452e4750590a2b5617adc40026f28d2f4903de15f1250e1d1c40bfd68ed55e"
  license all_of: ["LGPL-2.1-or-later", "GPL-2.0-or-later", "Apache-2.0"]

  # libsecret doesn't use GNOME's "even-numbered minor is stable" version
  # scheme. This regex is the same as the one generated by the `Gnome` strategy
  # but it's necessary to avoid the related version scheme logic.
  livecheck do
    url :stable
    regex(/libsecret[._-]v?(\d+(?:\.\d+)+)\.t/i)
  end

  bottle do
    sha256 cellar: :any, arm64_tahoe:   "ee55da32af1c04e94e871fc0bc012524bf46c64adf3012991a6c0e2fae089135"
    sha256 cellar: :any, arm64_sequoia: "79874cb427ebe11a1e139f777e566d34c8be68ca97a25020f128c8052d7b2f7b"
    sha256 cellar: :any, arm64_sonoma:  "cc698c5f2d961f9f09a8ba413bd5dc05f138714f2eabf2317f508cc5672ac0c1"
    sha256 cellar: :any, arm64_ventura: "27509297a2f39d82ec33dbd654c24927bcbcf05e117cc1f3fcd32b31038188a8"
    sha256 cellar: :any, sonoma:        "dde2b7e3ab77bca9a397532ff32e89a715ae2fca9121c76296fdd6d88310b5f0"
    sha256 cellar: :any, ventura:       "bad849e6cac866d1140597af61bbd3770fca70af8900b4fb86ad82d1ac9725bd"
    sha256               arm64_linux:   "d7a6d2ab018cc1b477f6b188069ee30fff3a18611f8135888b01e8654dad7f91"
    sha256               x86_64_linux:  "223ea87975e39fd3bb9ef2d04ea31c1c13b7f797199db96d2d2796b7e7856bf5"
  end

  depends_on "docbook-xsl" => :build
  depends_on "gettext" => :build
  depends_on "gobject-introspection" => :build
  depends_on "meson" => :build
  depends_on "ninja" => :build
  depends_on "pkgconf" => [:build, :test]
  depends_on "vala" => :build
  depends_on "glib"
  depends_on "libgcrypt"
  uses_from_macos "libxslt" => :build
  uses_from_macos "expat"

  on_macos do
    depends_on "gettext"
  end

  def install
    ENV["XML_CATALOG_FILES"] = "#{etc}/xml/catalog"

    system "meson", "setup", "build", "-Dbashcompdir=#{bash_completion}",
                                      "-Dgtk_doc=false",
                                      *std_meson_args
    system "meson", "compile", "-C", "build", "--verbose"
    system "meson", "install", "-C", "build"
  end

  test do
    (testpath/"test.c").write <<~C
      #include <libsecret/secret.h>

      const SecretSchema * example_get_schema (void) G_GNUC_CONST;

      const SecretSchema *
      example_get_schema (void)
      {
          static const SecretSchema the_schema = {
              "org.example.Password", SECRET_SCHEMA_NONE,
              {
                  {  "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
                  {  "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
                  {  "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
                  {  "NULL", 0 },
              }
          };
          return &the_schema;
      }

      int main()
      {
          example_get_schema();
          return 0;
      }
    C

    flags = shell_output("pkgconf --cflags --libs libsecret-1").chomp.split
    system ENV.cc, "test.c", "-o", "test", *flags
    system "./test"
  end
end
